package org.Annotation;

import org.reflections.Reflections;
import org.reflections.scanners.FieldAnnotationsScanner;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.TypeAnnotationsScanner;

import java.lang.annotation.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Set;
import java.util.function.Predicate;

public class TestAnnotation {

    public static void test() {
        System.out.println("Person has exist Report " + Person.class.isAnnotationPresent(Report.class));
        try {
            Method method = Person.class.getMethod("walk");
            System.out.println("walk has exist Report = " + method.isAnnotationPresent(Report.class));
            new Person("Jack", "New York New York");
//            check(person);
//            Reflections reflections = new Reflections("org.Annotation", new FieldAnnotationsScanner());
            Reflections reflections = new Reflections("org.Annotation", new TypeAnnotationsScanner(), new SubTypesScanner());
            Set<Class<? extends Object>> typeSet = reflections.getTypesAnnotatedWith(Report.class);
//            Set<Field> set = reflections.getFieldsAnnotatedWith(Range.class);
//            for (Object object: typeSet) {
////                if (object instanceof Person) {
//                    check((Person) object);
////                }
//            }
//            for (Field field: set) {
//
//            }
        } catch (NoSuchMethodException exception) {
            System.out.println("test no method");
        } catch (ReflectiveOperationException e) {
//            throw new RuntimeException(e);
            System.out.println("ReflectiveOperationException");
        }


    }

    static void check(Person person) throws IllegalArgumentException, ReflectiveOperationException {
        // 遍历所有Field:
        for (Field field : person.getClass().getFields()) {
            // 获取Field定义的@Range:
            Range range = field.getAnnotation(Range.class);
            // 如果@Range存在:
            if (range != null) {
                // 获取Field的值:
                Object value = field.get(person);
                // 如果值是String:
//                if (value instanceof String s) {
//                    // 判断值是否满足@Range的min/max:
//                    if (s.length() < range.min() || s.length() > range.max()) {
//                        StringBuilder stringBuilder = new StringBuilder("Invalid field: ");
//                        stringBuilder.append(field.getName());
//                        stringBuilder.append(" length is ");
//                        stringBuilder.append(s.length());
//                        stringBuilder.append(" limit max= ");
//                        stringBuilder.append(range.max());
//                        throw new IllegalArgumentException(stringBuilder.toString());
//                    }
//                }
            }
        }
    }

    @Target({
            ElementType.METHOD,
            ElementType.TYPE,
            ElementType.FIELD})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Report {
        int type() default 0;

        String level() default "info";

        String value() default "";
    }

    @Report
    static class Person {
        @Range(min = 1, max = 20)
        public String name;
        @Range(max = 10)
        public String city;

        @Report(type = 1, level = "error", value = "666")
        public String walk() {
            return "walk";
        }

        Person(String name, String city) {
            this.name = name;
            this.city = city;
        }
    }


    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface Range {
        int min() default 0;

        int max() default 255;
    }

}


